package com.vchello.service.log;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.vchello.bean.entrepren.ProDelivery;
import com.vchello.bean.entrepren.ProDeliveryMilepost;
import com.vchello.bean.entrepren.ProDeliveryOrganization;
import com.vchello.bean.entrepren.ProDeliveryRecord;
import com.vchello.service.entrepren.ProDeliveryRecordService;
import com.vchello.util.Utility;

/**
 * 
 * @author power
 *
 */
@Component
@Aspect
public class ProDeliveryRecordLog {
	
	@Autowired
	private ProDeliveryRecordService proDeliveryRecordService;

	/**
	 * 添加业务逻辑方法切入点
	 */
	@Pointcut("execution(* com.vchello.service.entrepren.ProDeliveryService.createDelivery*(..))")
	public void insertRecordLogCall() {
	
	}
	//proDeliveryOrganizations,proDeliveryMileposts
	@AfterReturning(value = "insertRecordLogCall() &&args(entity,proDeliveryOrganizations,proDeliveryMileposts,userId,deliveryId)", argNames = "entity,proDeliveryOrganizations,proDeliveryMileposts,userId,deliveryId")
	public void insertRecordLogCalls(JoinPoint joinPoint, ProDelivery entity, List<ProDeliveryOrganization> proDeliveryOrganizations,
			List<ProDeliveryMilepost> proDeliveryMileposts, int userId, int deliveryId) throws Throwable {
		if (joinPoint.getArgs() == null) {
			return;
		}
		String methodName = joinPoint.getSignature().getName();
		System.out.println(methodName);
		proDeliveryRecordService.saveProDliveryRecord(new ProDeliveryRecord(entity.getUserId(), deliveryId, entity.getId(), Utility.getSimpleFormatedDayNow(), 2));
		System.out.println("开始保存日志");
	}

	@Before(value = "insertRecordLogCall() &&args(entity,deliveryId)", argNames = "entity,deliveryId")
	public void before(JoinPoint joinPoint, ProDelivery entity, int deliveryId) {
		System.out.println("beforeSing-准备开始保存投递项目记录");
	}

	@Around(value = "execution(* com.vchello.service.entrepren.ProDeliveryService.createDelivery*(..)) &&args(entity,proDeliveryOrganizations,proDeliveryMileposts,userId,deliveryId)", argNames = "entity,proDeliveryOrganizations,proDeliveryMileposts,userId,deliveryId")
	public Object doConcurrentOperation(ProceedingJoinPoint pjp, ProDelivery entity, List<ProDeliveryOrganization> proDeliveryOrganizations,
			List<ProDeliveryMilepost> proDeliveryMileposts, int userId, int deliveryId) throws Throwable {
		Object result = null;
		String methodName = pjp.getSignature().getName();
		try {
			System.out.println("The method " + methodName + " begins with =" + entity);
			// if(status != 1) {
			result = pjp.proceed();
			System.out.println(result);
			System.out.println("The method " + methodName + " ends with " + Arrays.asList(pjp.getArgs()));
			// }
		} catch (Throwable e) {
			System.out.println("The method " + methodName + " occurs expection : " + e);
			throw new RuntimeException(e);
		}
		System.out.println("The method " + methodName + " ends");
		System.out.println("result=" + result);
		return result;
	}
}
